


package com.primecodegen.common.ObjectProperties;

/**
 *----------------------------------------------------------
 *----------------------------------------------------------
 * @author Mohammad Awwad
 * Version: v0.0
 * contact: mhmdawwad@gmail.com
 *-----------------------------------------------------------
 *-----------------------------------------------------------
 */

import java.sql.*;
import java.util.*;
import com.primecodegen.common.base.*;
import com.primecodegen.util.*;
import java.util.logging.*;

public class ObjectPropertiesDAO extends BaseDAO {

       protected static String TblName = "Object_Properties";
       protected java.sql.Connection objectPropertiesConn;
       protected static final Logger logger = Logger.getLogger (ObjectPropertiesDAO.class.getName ());

       public ObjectPropertiesDAO () {
              super ();
       }

       public int insert ( IBaseTable objectPropertiesTable ) throws Exception {


              final boolean isConnSupplied = ( objectPropertiesConn != null );
              ObjectPropertiesTable newRecord = ( ObjectPropertiesTable ) objectPropertiesTable;
              int insertValue = ComConstants.UNREALISTIC_INT;
              Connection con = null;
              PreparedStatement PrepSt = null;
              String sInsertSQL = null;
              StringBuffer sbSQL = new StringBuffer ();

              if ( objectPropertiesTable == null ) {
                     throw new Exception ("DAOException in ObjectPropertiesDAO class :objectPropertiesTable is Null");
              }
              sInsertSQL = "INSERT INTO " + ObjectPropertiesDAO.TblName + " (";
              try {
                     con = isConnSupplied ? objectPropertiesConn : ResourceManager.getConnection ();

                     String sComma = "";
                     int iColCount = 0;
		

								 if (!util.isEmpty(newRecord.getID())) {
								iColCount = iColCount + 1;
								sbSQL.append(sComma).append(ObjectPropertiesDBCols.ID);
								sComma = ",";
									}
					
								 if (!util.isEmpty(newRecord.getMainPrjID())) {
								iColCount = iColCount + 1;
								sbSQL.append(sComma).append(ObjectPropertiesDBCols.MAINPRJID);
								sComma = ",";
									}
					
								 if (!util.isEmpty(newRecord.getCode())) {
								iColCount = iColCount + 1;
								sbSQL.append(sComma).append(ObjectPropertiesDBCols.CODE);
								sComma = ",";
									}
					
								 if (!util.isEmpty(newRecord.getType())) {
								iColCount = iColCount + 1;
								sbSQL.append(sComma).append(ObjectPropertiesDBCols.TYPE);
								sComma = ",";
									}
					
								 if (!util.isEmpty(newRecord.getDescr())) {
								iColCount = iColCount + 1;
								sbSQL.append(sComma).append(ObjectPropertiesDBCols.DESCR);
								sComma = ",";
									}
					 
			
		     sbSQL.append (") VALUES (");
                     sComma = "";
                     for ( int i = 0 ; i < iColCount ; i++ ) {
                            sbSQL.append (sComma).append ("?");
                            sComma = ",";
                     }
                     sbSQL.append (");");

                     sInsertSQL = sInsertSQL + sbSQL.toString ();

                     PrepSt = con.prepareStatement (sInsertSQL ,
                             Statement.RETURN_GENERATED_KEYS);
                     int i = 1;



			 						      if (!util.isEmpty(newRecord.getID())) {
			 							PrepSt.setInt(i++, newRecord.getID());
			 						    }
			 			
			 						      if (!util.isEmpty(newRecord.getMainPrjID())) {
			 							PrepSt.setInt(i++, newRecord.getMainPrjID());
			 						    }
			 			
			 					   if (!util.isEmpty(newRecord.getCode().trim())) {
			 							PrepSt.setString(i++, newRecord.getCode().trim());
			 						    }
			 			
			 					   if (!util.isEmpty(newRecord.getType().trim())) {
			 							PrepSt.setString(i++, newRecord.getType().trim());
			 						    }
			 			
			 					   if (!util.isEmpty(newRecord.getDescr().trim())) {
			 							PrepSt.setString(i++, newRecord.getDescr().trim());
			 						    }
			 			 

		     insertValue = PrepSt.executeUpdate ();
                     ResultSet rsIDJustInserted = PrepSt.getGeneratedKeys ();
                     if ( rsIDJustInserted.next () ) {
                            insertValue = rsIDJustInserted.getInt (1);
                     }



              } catch ( SQLException sqlEx ) {
                     logger.log (Level.WARNING , "SQLException in ObjectPropertiesDAO.insert method Message: {0}" , sqlEx.getMessage ());
                     throw new Exception ("SQLException in ObjectPropertiesDAO.insert method");
              } catch ( Exception e ) {
                     logger.log (Level.WARNING, "Exception in ObjectPropertiesDAO.insert method Message: {0}", e.getMessage ());
                     throw new Exception ("Exception in ObjectPropertiesDAO.insert method");
              } finally {
                     ResourceManager.close (PrepSt);
                     if ( !isConnSupplied ) {
                            ResourceManager.close (con);
                     }
              }

              return insertValue;
       }

       public int deleteByID ( int IDPar ) throws Exception {

              final boolean isConnSupplied = ( objectPropertiesConn != null );
              int ID = ( int ) IDPar;
              Connection con = null;
              PreparedStatement stmnt = null;
              int noOfRowsDeleted = ComConstants.UNREALISTIC_INT;
              String deleteSQL = "DELETE FROM " + ObjectPropertiesDAO.TblName + " WHERE " + ObjectPropertiesDBCols.ID + " = ?";
              try {
                     con = isConnSupplied ? objectPropertiesConn : ResourceManager.getConnection ();
                     stmnt = con.prepareStatement (deleteSQL);
                     stmnt.setInt (1, ID);

                     noOfRowsDeleted = stmnt.executeUpdate ();



              } catch ( SQLException sqlEx ) {
                     logger.log (Level.WARNING, "SQLException in ObjectPropertiesDAO.DeleteByID method Message: {0}", sqlEx.getMessage ());
                     throw new Exception ("SQLException in ObjectPropertiesDAO.deleteByID method"
                             + sqlEx.getMessage ());
              } catch ( Exception e ) {
                     logger.log (Level.WARNING, "Exception in ObjectPropertiesDAO.DeleteByID method Message: {0}", e.getMessage ());
                     throw new Exception ("Exception in ObjectPropertiesDAO.deleteByID method"
                             + e.getMessage ());
              } finally {
                     if ( !isConnSupplied ) {
                            ResourceManager.close (con);
                     }
              }
              return noOfRowsDeleted;
       }

       public int updateByCriterion ( IBaseTable objectPropertiesSrchCriterionDTOPar ,
               IBaseTable updatedRecordPar ) throws Exception {

              final boolean isConnSupplied = ( objectPropertiesConn != null );
              ObjectPropertiesTable objectPropertiesSrchCriterionDTO = ( ObjectPropertiesTable ) objectPropertiesSrchCriterionDTOPar;
              ObjectPropertiesTable updatedRecord = ( ObjectPropertiesTable ) updatedRecordPar;
              int iNoOfRecordsUpdated = -1;
              Connection con = null;
              PreparedStatement stmnt = null;
              try {

                     con = isConnSupplied ? objectPropertiesConn : ResourceManager.getConnection ();
                     StringBuffer sbUpdateSQLStmnt = new StringBuffer ();
                     String sComma = "";
                     sbUpdateSQLStmnt.append ("UPDATE ").append (ObjectPropertiesDAO.TblName).append (" SET ");



		 		          if ( !util.isEmpty (updatedRecord.getID ()) ) {
		                             sbUpdateSQLStmnt
		                                     .append (sComma)
		                                     .append (ObjectPropertiesDBCols.ID + " = ")
		                                     .append ("?");
		                             sComma = ",";
		                      }
		 		
		 		          if ( !util.isEmpty (updatedRecord.getMainPrjID ()) ) {
		                             sbUpdateSQLStmnt
		                                     .append (sComma)
		                                     .append (ObjectPropertiesDBCols.MAINPRJID + " = ")
		                                     .append ("?");
		                             sComma = ",";
		                      }
		 		
		 		          if ( !util.isEmpty (updatedRecord.getCode ()) ) {
		                             sbUpdateSQLStmnt
		                                     .append (sComma)
		                                     .append (ObjectPropertiesDBCols.CODE + " = ")
		                                     .append ("?");
		                             sComma = ",";
		                      }
		 		
		 		          if ( !util.isEmpty (updatedRecord.getType ()) ) {
		                             sbUpdateSQLStmnt
		                                     .append (sComma)
		                                     .append (ObjectPropertiesDBCols.TYPE + " = ")
		                                     .append ("?");
		                             sComma = ",";
		                      }
		 		
		 		          if ( !util.isEmpty (updatedRecord.getDescr ()) ) {
		                             sbUpdateSQLStmnt
		                                     .append (sComma)
		                                     .append (ObjectPropertiesDBCols.DESCR + " = ")
		                                     .append ("?");
		                             sComma = ",";
		                      }
		 		 
		
                     sbUpdateSQLStmnt.append (getUpdateWhereClause (objectPropertiesSrchCriterionDTO));

                     stmnt=con.prepareStatement (sbUpdateSQLStmnt.toString ());
		     int i=1;

             		if ( !util.isEmpty (updatedRecord.getID ()) ) {
                                     stmnt.setInt (i++, updatedRecord.getID ());
                                  }
             		
             		if ( !util.isEmpty (updatedRecord.getMainPrjID ()) ) {
                                     stmnt.setInt (i++, updatedRecord.getMainPrjID ());
                                  }
             		
             		if ( !util.isEmpty (updatedRecord.getCode ()) ) {
                                     stmnt.setString (i++, updatedRecord.getCode ());
                                  }
             		
             		if ( !util.isEmpty (updatedRecord.getType ()) ) {
                                     stmnt.setString (i++, updatedRecord.getType ());
                                  }
             		
             		if ( !util.isEmpty (updatedRecord.getDescr ()) ) {
                                     stmnt.setString (i++, updatedRecord.getDescr ());
                                  }
             		 
                   

                     iNoOfRecordsUpdated = stmnt.executeUpdate ();

              } catch ( Exception e ) {
                     logger.log (Level.WARNING, "Exception in ObjectPropertiesDAO.updateByCriterion method Message: {0}", e.getMessage ());
                     throw new Exception (
                             "Exception in ObjectPropertiesDAO.updateByCriterion method ");
              } finally {
                     if ( !isConnSupplied ) {
                            ResourceManager.close (con);
                     }
              }

              return iNoOfRecordsUpdated;
       }

      public int updateByID ( IBaseTable newRecordPar ) throws Exception {
              ObjectPropertiesTable newRecord = ( ObjectPropertiesTable ) newRecordPar;
              if ( newRecord != null ) {
                     ObjectPropertiesTable contentsCategorySrchCriterionDTO = new ObjectPropertiesTable ();
                     contentsCategorySrchCriterionDTO.setID (newRecord.getID ());
                     return this.updateByCriterion (contentsCategorySrchCriterionDTO , newRecord);
              } else {
                     logger.log (Level.WARNING , "Exception in method ObjectPropertiesDAO.update.newRecord passed is null.");
                     throw new Exception (
                             "Exception in method ObjectPropertiesDAO.update.newRecord passed is null.");
              }
       }
       private String getUpdateWhereClause ( ObjectPropertiesTable objectPropertiesSrchCriterionDTO ) {
              String sWhere = "";
              if ( objectPropertiesSrchCriterionDTO.getID () != ComConstants.UNREALISTIC_INT ) {
                     sWhere = " WHERE ID = " + objectPropertiesSrchCriterionDTO.getID ();
              }
              return sWhere;
       }

 

       public IBaseTable getByID ( int ID ) throws Exception {

              if ( !util.isEmpty (ID) ) {
                     ObjectPropertiesTable objectPropertiesSrchCriterionDTO = new ObjectPropertiesTable ();
                     objectPropertiesSrchCriterionDTO.setID (ID);
                     objectPropertiesSrchCriterionDTO.setFilter (true);
                     ArrayList<ObjectPropertiesTable> objectPropertiesTables = this.getByCriterionOrderedBy (
                             objectPropertiesSrchCriterionDTO , null);
                     ObjectPropertiesTable objectPropertiesTable = null;
                     if ( objectPropertiesTables != null ) {
                            Iterator<ObjectPropertiesTable> it = objectPropertiesTables.iterator ();
                            while ( it.hasNext () ) {
                                   objectPropertiesTable = ( ObjectPropertiesTable ) it.next ();
                            }
                     }

                     return objectPropertiesTable;
              } else {
                     logger.log (Level.WARNING , "Exception in method ObjectPropertiesDAO.getByID.ID passed is unrealistic.");
                     throw new Exception (
                             "Exception in method ObjectPropertiesDAO.getByID.ID passed is unrealistic.");
              }
       }


           public boolean deleteMainProjectIsSafe(int MainProjectID) throws Exception {
        ArrayList<ObjectPropertiesTable> objectPropertiesTables = new ArrayList<ObjectPropertiesTable>();
        if (!util.isEmpty(MainProjectID)) {
            ObjectPropertiesTable objectPropertiesSrchCriterionDTO = new ObjectPropertiesTable();
            objectPropertiesSrchCriterionDTO.setMainPrjID(MainProjectID);
            objectPropertiesSrchCriterionDTO.setFilter(true);
            objectPropertiesTables = this.getByCriterionOrderedBy(objectPropertiesSrchCriterionDTO, null);

            if (objectPropertiesTables.size()>0) {
                return false;//Delete is SAFE, Group Empty
            } else {
                return true;
            }

        } else {
            logger.log(Level.WARNING, "Exception in method ObjectPropertiesDAO.getByID.ID passed is unrealistic.");
            throw new Exception(
                    "Exception in method ObjectPropertiesDAO.getByID.ID passed is unrealistic.");
        }
    }

       public ArrayList<ObjectPropertiesTable> getByCriterionOrderedBy (
               IBaseTable objectPropertiesSrchCriterionDTOPar , ColToOrderBy colToOrderBy )
               throws Exception {


              final boolean isConnSupplied = ( objectPropertiesConn != null );
              ObjectPropertiesTable objectPropertiesSrchCriterionDTO = ( ObjectPropertiesTable ) objectPropertiesSrchCriterionDTOPar;
              Connection con = null;
              PreparedStatement stmnt = null;
              ResultSet rs = null;

              String sWhereClause = "";

              String sOrderBy = "";
              if ( colToOrderBy != null && colToOrderBy.Name != null ) {
                     sOrderBy = this.getOrderBy (colToOrderBy);
              }
              if ( objectPropertiesSrchCriterionDTO != null && objectPropertiesSrchCriterionDTO.isFilter ()) {
                     try {
                            sWhereClause = this.getRetrieveWhereClause (objectPropertiesSrchCriterionDTO);
                     } catch ( Exception e ) {
                            System.out.println ("getByCriterionOrderedBy.Exception..");
                            throw new Exception (" ObjectProperties.getByCriterionOrderedBy.Exception:  "+ e.getMessage ());
                     }

                     if ( sWhereClause != null && !sWhereClause.trim ().equalsIgnoreCase ("") ) {
                            sWhereClause = " WHERE " + sWhereClause;
                     }
              }
              ArrayList<ObjectPropertiesTable> objectPropertiesTables = new ArrayList<ObjectPropertiesTable> ();

              try {
                     con = isConnSupplied ? objectPropertiesConn : ResourceManager.getConnection ();

                     String sSelectSQL = "SELECT * FROM   " + TblName;
//                       StringBuilder sSelectSQL = new StringBuilder();
//                        sSelectSQL.append("SELECT ").append(" roles.ID ").append(" AS ").append(RolesDBCols.ID).append(", roles.Code ").append(" AS ").append(RolesDBCols.CODE).append(", roles.Name ").append(" AS ").append(RolesDBCols.NAME).append(", roles.Description ").append(" AS ").append(RolesDBCols.DESCRIPTION).append(", roles.Details ").append(" AS ").append(RolesDBCols.DETAILS).append(", roles.IsActive ").append(" AS ").append(RolesDBCols.ISACTIVE).append(", roles.IsVisible ").append(" AS ").append(RolesDBCols.ISVISIBLE).append(", roles_group.ID ").append(" AS ").append(RolesDBCols.ROLEGROUPID).append(", roles_group.GroupName ").append(" AS ").append(RolesDBCols.ROLEGROUPNAME).append(" FROM ").append(" roles ").append("INNER JOIN roles_group ON (roles.RolesGroupID = roles_group.ID)");

                     stmnt = con.prepareStatement (sSelectSQL + sWhereClause + sOrderBy);
			

			

                     if ( objectPropertiesSrchCriterionDTO != null && objectPropertiesSrchCriterionDTO.isFilter ()) {
                            int colCount = 1;
				

						  if ( !util.isEmpty (objectPropertiesSrchCriterionDTO.getID ()) ) {
			                                   stmnt.setInt (colCount++ , objectPropertiesSrchCriterionDTO.getID ());
			                            }
						
						  if ( !util.isEmpty (objectPropertiesSrchCriterionDTO.getMainPrjID ()) ) {
			                                   stmnt.setInt (colCount++ , objectPropertiesSrchCriterionDTO.getMainPrjID ());
			                            }
						
						  if ( !util.isEmpty (objectPropertiesSrchCriterionDTO.getCode ()) ) {
			                                   stmnt.setString (colCount++ , objectPropertiesSrchCriterionDTO.getCode ());
			                            }
						
						  if ( !util.isEmpty (objectPropertiesSrchCriterionDTO.getType ()) ) {
			                                   stmnt.setString (colCount++ , objectPropertiesSrchCriterionDTO.getType ());
			                            }
						
						  if ( !util.isEmpty (objectPropertiesSrchCriterionDTO.getDescr ()) ) {
			                                   stmnt.setString (colCount++ , objectPropertiesSrchCriterionDTO.getDescr ());
			                            }
						 
		  }

                     rs = stmnt.executeQuery ();
                     ObjectPropertiesTable objectPropertiesTable = null;

                     while ( rs.next () ) {
                            objectPropertiesTable = new ObjectPropertiesTable ();


						  
							 objectPropertiesTable.setID (rs.getInt (ObjectPropertiesDBCols.ID));
						
						  
							 objectPropertiesTable.setMainPrjID (rs.getInt (ObjectPropertiesDBCols.MAINPRJID));
						
						  
							objectPropertiesTable.setCode (util.removeNullFrom (rs.getString  (ObjectPropertiesDBCols.CODE)));
						
						  
							objectPropertiesTable.setType (util.removeNullFrom (rs.getString  (ObjectPropertiesDBCols.TYPE)));
						
						  
							objectPropertiesTable.setDescr (util.removeNullFrom (rs.getString  (ObjectPropertiesDBCols.DESCR)));
						 
                  
			objectPropertiesTables.add (objectPropertiesTable);
                        objectPropertiesTable = null;
                     }

              } catch ( SQLException sqlEx ) {
                     logger.log (Level.WARNING, "SQLException in ObjectPropertiesDAO.insert method Message: {0}", sqlEx.getMessage ());
                     throw new Exception (
                             "SQLException in method:ObjectPropertiesDAO.getByCriterionOrderedBy"
                             + sqlEx.getMessage ());
              } catch ( Exception e ) {
                     logger.log (Level.WARNING, "Exception in ObjectPropertiesDAO.insert method Message: {0}", e.getMessage ());
                     throw new Exception (
                             "Exception in method:ObjectPropertiesDAO.getByCriterionOrderedBy"
                             + e.getMessage ());
              } finally {
                     if ( !isConnSupplied ) {
                            ResourceManager.close (con);
                     }
              }
              return objectPropertiesTables;
       }

       private String getRetrieveWhereClause ( ObjectPropertiesTable objectPropertiesSrchCriterionDTO )
               throws Exception {


              StringBuffer sbWhereClause = new StringBuffer ();
              String sAnd = "";
              boolean bPrevColExistsInWhereClause = false;
              boolean bAtLeastOneFieldExists = false;

              if ( !util.isEmpty (objectPropertiesSrchCriterionDTO.getID ()) ) {
                     bAtLeastOneFieldExists = true;
                     sbWhereClause.append (" ID =").append (" ? ");
                     bPrevColExistsInWhereClause = true;
              }

              if ( objectPropertiesSrchCriterionDTO.Opr != null ) {
		


		                                        if ( !util.isEmpty (objectPropertiesSrchCriterionDTO.Opr.getIDOpr ().trim ()) && !util.isEmpty (objectPropertiesSrchCriterionDTO.getID ()) ) {
		                                            bAtLeastOneFieldExists = true;
		                                            if ( bPrevColExistsInWhereClause ) {
		                                                   sAnd = " AND ";
		                                            } else {
		                                                   sAnd = "";
		                                            }
		                                           sbWhereClause.append (sAnd).append (" " + ObjectPropertiesDBCols.ID + "  ").append (objectPropertiesSrchCriterionDTO.Opr.getIDOpr ().trim ()).append ("?");
		                                           bPrevColExistsInWhereClause = true;
		                                     }

				
		                                        if ( !util.isEmpty (objectPropertiesSrchCriterionDTO.Opr.getMainPrjIDOpr ().trim ()) && !util.isEmpty (objectPropertiesSrchCriterionDTO.getMainPrjID ()) ) {
		                                            bAtLeastOneFieldExists = true;
		                                            if ( bPrevColExistsInWhereClause ) {
		                                                   sAnd = " AND ";
		                                            } else {
		                                                   sAnd = "";
		                                            }
		                                           sbWhereClause.append (sAnd).append (" " + ObjectPropertiesDBCols.MAINPRJID + "  ").append (objectPropertiesSrchCriterionDTO.Opr.getMainPrjIDOpr ().trim ()).append ("?");
		                                           bPrevColExistsInWhereClause = true;
		                                     }

				
		                                 if ( !util.isEmpty (objectPropertiesSrchCriterionDTO.Opr.getCodeOpr ().trim ()) && !util.isEmpty (objectPropertiesSrchCriterionDTO.getCode ().trim ()) ) {
		                                            bAtLeastOneFieldExists = true;
		                                            if ( bPrevColExistsInWhereClause ) {
		                                                   sAnd = " AND ";
		                                            } else {
		                                                   sAnd = "";
		                                            }
		                                            if ( objectPropertiesSrchCriterionDTO.Opr.getCodeOpr ().trim ().equalsIgnoreCase ("LIKE") ) {
		                                                   sbWhereClause.append (sAnd).append (" " + ObjectPropertiesDBCols.CODE + " LIKE").append (" ? ");
		                                            } else {
		                                                   sbWhereClause.append (sAnd).append (" " + ObjectPropertiesDBCols.CODE + "  ").append (objectPropertiesSrchCriterionDTO.Opr.getCodeOpr ().trim ()).append ("?");
		                                            }
		                                            bPrevColExistsInWhereClause = true;
		                                     }

				
		                                 if ( !util.isEmpty (objectPropertiesSrchCriterionDTO.Opr.getTypeOpr ().trim ()) && !util.isEmpty (objectPropertiesSrchCriterionDTO.getType ().trim ()) ) {
		                                            bAtLeastOneFieldExists = true;
		                                            if ( bPrevColExistsInWhereClause ) {
		                                                   sAnd = " AND ";
		                                            } else {
		                                                   sAnd = "";
		                                            }
		                                            if ( objectPropertiesSrchCriterionDTO.Opr.getTypeOpr ().trim ().equalsIgnoreCase ("LIKE") ) {
		                                                   sbWhereClause.append (sAnd).append (" " + ObjectPropertiesDBCols.TYPE + " LIKE").append (" ? ");
		                                            } else {
		                                                   sbWhereClause.append (sAnd).append (" " + ObjectPropertiesDBCols.TYPE + "  ").append (objectPropertiesSrchCriterionDTO.Opr.getTypeOpr ().trim ()).append ("?");
		                                            }
		                                            bPrevColExistsInWhereClause = true;
		                                     }

				
		                                 if ( !util.isEmpty (objectPropertiesSrchCriterionDTO.Opr.getDescrOpr ().trim ()) && !util.isEmpty (objectPropertiesSrchCriterionDTO.getDescr ().trim ()) ) {
		                                            bAtLeastOneFieldExists = true;
		                                            if ( bPrevColExistsInWhereClause ) {
		                                                   sAnd = " AND ";
		                                            } else {
		                                                   sAnd = "";
		                                            }
		                                            if ( objectPropertiesSrchCriterionDTO.Opr.getDescrOpr ().trim ().equalsIgnoreCase ("LIKE") ) {
		                                                   sbWhereClause.append (sAnd).append (" " + ObjectPropertiesDBCols.DESCR + " LIKE").append (" ? ");
		                                            } else {
		                                                   sbWhereClause.append (sAnd).append (" " + ObjectPropertiesDBCols.DESCR + "  ").append (objectPropertiesSrchCriterionDTO.Opr.getDescrOpr ().trim ()).append ("?");
		                                            }
		                                            bPrevColExistsInWhereClause = true;
		                                     }

				


              }

              if ( sbWhereClause.toString ().trim ().length () <= 0 ) {
                     logger.log (Level.WARNING , "getRetrieveWhereClause Cannot return Null  ");
                     throw new Exception ("getRetrieveWhereClause Cannot return Null  ");
              } else {
                     return sbWhereClause.toString ().trim ();
              }
       }

       private String getOrderBy ( ColToOrderBy colToOrderBy ) {
              String sOrderBy = " Order By ";

              sOrderBy = sOrderBy + colToOrderBy.Name;

              if ( colToOrderBy.OrderType != null ) {
                     sOrderBy = sOrderBy + " " + colToOrderBy.OrderType;
              }
              return sOrderBy;
       }

       public int deleteByCriterion ( IBaseTable recordsToDeleteCritPar )
               throws Exception {

              final boolean isConnSupplied = ( objectPropertiesConn != null );
              ObjectPropertiesTable recordsToDeleteCrit = ( ObjectPropertiesTable ) recordsToDeleteCritPar;
              int iNoOfRecordsDeleted = -1;
              Connection con = null;
              PreparedStatement stmnt = null;

              StringBuffer sbDeleteSQLStmnt = new StringBuffer ();
              sbDeleteSQLStmnt
                      .append ("DELETE from ")
                      .append (ObjectPropertiesDAO.TblName)
                      .append (" WHERE ");

              String sWhereClause = "";
              if ( recordsToDeleteCrit != null &&  recordsToDeleteCrit.isFilter ()) {
                     try {
                            sWhereClause = this.getRetrieveWhereClause (recordsToDeleteCrit);
                     } catch ( Exception e ) {
                            System.out.println ("getByCriterionOrderedBy.Exception..");
                            throw new Exception (" ObjectProperties.deleteByCriterion.Exception:  "
                                    + e.getMessage ());
                     }

                     if ( sWhereClause != null
                             && !sWhereClause.trim ().equalsIgnoreCase ("") ) {
                            sWhereClause = " WHERE " + sWhereClause;
                     }

                     sbDeleteSQLStmnt.append (sWhereClause);
              }
              try {
                     con = isConnSupplied ? objectPropertiesConn : ResourceManager.getConnection ();
                     stmnt = con.prepareStatement (sbDeleteSQLStmnt.toString ());

                     if ( recordsToDeleteCrit != null &&  recordsToDeleteCrit.isFilter ()) {

                     }
                   //  iNoOfRecordsDeleted = stmnt.executeUpdate ();



                     return iNoOfRecordsDeleted;
              } catch ( SQLException sqlEx ) {
                     logger.log (Level.WARNING, "SQLException in ObjectPropertiesDAO.deleteByCriterion method Message: {0}", sqlEx.getMessage ());
                     throw new Exception (
                             "SQLException in ObjectPropertiesDAO.deleteByCriterion method");
              } catch ( Exception e ) {
                     logger.log (Level.WARNING, "Exception in ObjectPropertiesDAO.deleteByCriterion method Message: {0}", e.getMessage ());
                     throw new Exception (
                             "Exception in ObjectPropertiesDAO.deleteByCriterion method");
              } finally {

                     if ( !isConnSupplied ) {
                            ResourceManager.close (con);
                     }
              }
       }
}
